const fp = require('lodash/fp')

const cars = [
    { name: 'Ferrari FF', horsepower: 660, dollar_value: 700000, in_stock: true },
    { name: 'Spyker C12 Zagato', horsepower: 650, dollar_value: 648000, in_stock: false },
    { name: 'Jaguar XKR-S', horsepower: 550, dollar_value: 132000, in_stock: false },
    { name: 'Audi R8', horsepower: 525, dollar_value: 114200, in_stock: false },
    { name: 'Aston Martion One-77', horsepower: 750, dollar_value: 1850000, in_stock: true },
    { name: 'Pagani Huayra', horsepower: 700, dollar_value: 1300000, in_stock: false },
]

// 1. 使用函数组合fp.flowRight()重新实现下面这个函数

const res = fp.flowRight(fp.prop('in_stock'), fp.last)
console.log(res(cars))

// 2. 使用fp.flowRight()、fp.prop()、fp.first()获取第一个car的name

const firstCarName = fp.flowRight(fp.prop('name'), fp.first)
console.log(firstCarName(cars))

// 3. 使用帮助函数 _average 重构 averageDollarValue，使用函数组合的方式实现


let _average = function(xs) {
    return fp.reduce(fp.add, 0, xs) / xs.length
}

// let averageDollarValue = function(cars) {
//     let dollar_values = fp.map(function(car) {
//         return car.dollar_value
//     }, cars)
//     return _average(dollar_values)
// }
// console.log(averageDollarValue(cars))
const dollar_values = fp.map((car) => car.dollar_value) // 将 dollar_value 提取出一个数组
const averageDollarValue = fp.flowRight(_average, dollar_values)
console.log(averageDollarValue(cars))

// 4
function log(val) {
    console.log(val, 'a')
    return
}
const arr = ['Hello World', 'A D']
const _underscore = fp.replace(/\W+/g, '_')
const map = fp.map(_underscore) // 将数组中每一个元素的空格都替换成 '_'
const sanitzeNames = fp.flowRight(map)
console.log(sanitzeNames(arr))